# pip install alibabacloud_videoenhan20200320 -i https://pypi.org/simple
import urllib
import urllib.request
import ssl
import io
from urllib.request import urlopen
from alibabacloud_tea_openapi.models import Config
from alibabacloud_tea_util.models import RuntimeOptions
from alibabacloud_videoenhan20200320.client import Client as srClient
from alibabacloud_videoenhan20200320.models import SuperResolveVideoAdvanceRequest
from time import sleep
# pip install alibabacloud_viapi20230117 -i https://pypi.org/simple
from alibabacloud_viapi20230117.client import Client as gajrClient
from alibabacloud_viapi20230117.models import GetAsyncJobResultRequest
import json
import oss2 as oss
import os

input_url = 'https://mps-test.oss.cn-north-3.inspurcloudoss.com/2023031017285879235598430250.mp4'
# 全局取消证书验证
ssl._create_default_https_context = ssl._create_unverified_context
# 获取inspur的oss的文件
urllib.request.urlretrieve(input_url, 'media_process_test.mp4')
# 接口配置
config = Config(
    access_key_id='LTAI5t6GK5tGDsMU7jEGdEkM',
    access_key_secret='8H1pdFYccKdcDPciYKFtlZUsmCqnPM',
    endpoint='videoenhan.cn-shanghai.aliyuncs.com',
    region_id='cn-shanghai'
)
# 新建任务
img = open(r'media_process_test.mp4', 'rb')
super_resolve_video_request = SuperResolveVideoAdvanceRequest()
super_resolve_video_request.video_url_object = img
super_resolve_video_request.bit_rate = 5
runtime = RuntimeOptions()
try:
    client = srClient(config)
    response = client.super_resolve_video_advance(super_resolve_video_request, runtime)
    RequestId = json.loads(str(response.body).replace("'",'"'))['RequestId']
    print(RequestId)
except Exception as error:
    print(error)
    print(error.code)
# 查询任务状态
config = Config(
  access_key_id='LTAI5t6GK5tGDsMU7jEGdEkM',
  access_key_secret='8H1pdFYccKdcDPciYKFtlZUsmCqnPM',
  endpoint='viapi.cn-shanghai.aliyuncs.com',
  region_id='cn-shanghai'
)
while True:
    sleep(3)
    get_async_job_result_request = GetAsyncJobResultRequest(
        job_id=RequestId
    )
    runtime = RuntimeOptions()
    try:
        client = gajrClient(config)
        response = client.get_async_job_result_with_options(get_async_job_result_request, runtime)
        Status = json.loads(str(response.body).replace('"','').replace("'",'"'))['Data']['Status']
        print(Status)
        if Status=='PROCESS_SUCCESS':
            Result = json.loads(str(response.body).replace('"','').replace("'",'"'))['Data']['Result']
            ResultURL = Result[Result.find(':')+1:-1]
            print(ResultURL)
            break
    except Exception as error:
        print(error)
        print(error.code)
# 下载接口传回的文件
# 上传的对象存储